package com.example.farmer.Presenter;

import com.example.farmer.Database.ProductPublishDatabase;
import com.example.farmer.View.Product.ProductPostView;


public class ProductPostPresenter {

    private ProductPostView view;
    private ProductPublishDatabase productPublishDatabase;

    public ProductPostPresenter(ProductPostView mview) {
        view = mview;
        productPublishDatabase = new ProductPublishDatabase(view.getContext());
    }

    public void posts(String imageUri,String title, String producttype, String prices, String freight,
                      String shipaddress,String pushtype,String userNumber)
    {
        view.showLoading();
        if (getValidationErrorMessage(imageUri, title,  producttype,  prices,  freight,
                shipaddress, pushtype,userNumber)) {
            // 验证成功，执行发布逻辑
            long result = productPublishDatabase.insertPost( imageUri,title,  producttype,  prices,  freight,
                     shipaddress, pushtype,userNumber);
            if (result !=-1) {
                view.hideLoading();
//                List<ClassPost> posts = post_db.getPostsByType("/* 相应的类型 */");
                view.showPostSuccess();
            } else {
                view.hideLoading();
                view.showPostError("数据库插入失败，请稍后重试。");
            }
        } else {
            view.hideLoading();
            // 验证失败，显示所有错误信息
            view.showPostError("内容验证失败");
        }
    }


    private boolean getValidationErrorMessage(String pushtype,String title, String producttype, String prices, String freight,
                                              String shipaddress,String imageUri,String userNumber) {
        if (title.length() < 2 || title.length() > 15) {
            view.hideLoading();
            view.showPostError("货品标题必须在2到15个字符之间。\n");
            return false;
        }
        if (title.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("货品标题不能为空。\n");
            return false;
        }
        if (prices.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("货品单价不能为空。\n");
            return false;
        }
        if (imageUri ==null) {
            view.hideLoading();
            view.showPostError("图片不能为空。\n");
            return false;
        }
        if ( pushtype.trim().isEmpty()||producttype.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("请选择类型。\n");
            return false;
        }
        if (shipaddress.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("发货地址不能为空。\n");
            return false;
        }
        if (freight.trim().isEmpty()) {
            view.hideLoading();
            view.showPostError("货品运费不能为空。\n");
            return false;
        }

        // 检查内容是否只包含数字、字母、汉字和标点符号
        if (!isValidContent(title,shipaddress,freight)) {
            // 内容包含无效字符，发布失败并说明理由
            view.hideLoading();
            view.showPostError("内容只能包含数字、字母、汉字和标点符号");
            return false;
        }
        return isValidContent(title,shipaddress,freight);
    }


    private boolean isValidContent(String title,String shipaddress,String freight) {
        String regex = "^[\u4e00-\u9fa5a-zA-Z0-9,.!?;，。:：“‘() ]+$";
        return title.matches(regex)||shipaddress.matches(regex)||freight.matches(regex);
    }

}
